import { APIResult, getToken } from '@/requestErrorConfig';
import { request, RequestOptions } from '@umijs/max';

export const WEB_DOMAIN: Record<'DEV' | 'PROD' | 'SIT', string> = {
  DEV: 'http://111.229.218.72:16004',
  SIT: 'http://111.229.218.72:16004',
  PROD: '',
};

const myRequest = <T>(url: string, opts: RequestOptions = { method: 'GET' }, isAuth = true) => {
  const token = getToken();
  if (isAuth && !token) {
    return Promise.resolve<APIResult<T>>({
      rsCode: '401',
      msg: '请先登录',
      body: null as T,
    });
  }
  return request<APIResult<T>>(url, {
    ...opts,
  });
};

export default myRequest;
export const uploadImageApi = (data: FormData) => {
  return myRequest<IUploadResultItem>(
    '/admin_api/file/upload',
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data,
    },
    false,
  );
};

export type IUploadResultItem = {
  uploaded_file_id: number;
  url: string;
};
